{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "078b60dd",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sympy import *\n",
    "from sympy.abc import x,y,z,a,b,c,m,n,t,k,l"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "5f1f17e6",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on function fourier_series in module sympy.series.fourier:\n",
      "\n",
      "fourier_series(f, limits=None, finite=True)\n",
      "    Computes the Fourier trigonometric series expansion.\n",
      "    \n",
      "    Explanation\n",
      "    ===========\n",
      "    \n",
      "    Fourier trigonometric series of $f(x)$ over the interval $(a, b)$\n",
      "    is defined as:\n",
      "    \n",
      "    .. math::\n",
      "        \\frac{a_0}{2} + \\sum_{n=1}^{\\infty}\n",
      "        (a_n \\cos(\\frac{2n \\pi x}{L}) + b_n \\sin(\\frac{2n \\pi x}{L}))\n",
      "    \n",
      "    where the coefficients are:\n",
      "    \n",
      "    .. math::\n",
      "        L = b - a\n",
      "    \n",
      "    .. math::\n",
      "        a_0 = \\frac{2}{L} \\int_{a}^{b}{f(x) dx}\n",
      "    \n",
      "    .. math::\n",
      "        a_n = \\frac{2}{L} \\int_{a}^{b}{f(x) \\cos(\\frac{2n \\pi x}{L}) dx}\n",
      "    \n",
      "    .. math::\n",
      "        b_n = \\frac{2}{L} \\int_{a}^{b}{f(x) \\sin(\\frac{2n \\pi x}{L}) dx}\n",
      "    \n",
      "    The condition whether the function $f(x)$ given should be periodic\n",
      "    or not is more than necessary, because it is sufficient to consider\n",
      "    the series to be converging to $f(x)$ only in the given interval,\n",
      "    not throughout the whole real line.\n",
      "    \n",
      "    This also brings a lot of ease for the computation because\n",
      "    you do not have to make $f(x)$ artificially periodic by\n",
      "    wrapping it with piecewise, modulo operations,\n",
      "    but you can shape the function to look like the desired periodic\n",
      "    function only in the interval $(a, b)$, and the computed series will\n",
      "    automatically become the series of the periodic version of $f(x)$.\n",
      "    \n",
      "    This property is illustrated in the examples section below.\n",
      "    \n",
      "    Parameters\n",
      "    ==========\n",
      "    \n",
      "    limits : (sym, start, end), optional\n",
      "        *sym* denotes the symbol the series is computed with respect to.\n",
      "    \n",
      "        *start* and *end* denotes the start and the end of the interval\n",
      "        where the fourier series converges to the given function.\n",
      "    \n",
      "        Default range is specified as $-\\pi$ and $\\pi$.\n",
      "    \n",
      "    Returns\n",
      "    =======\n",
      "    \n",
      "    FourierSeries\n",
      "        A symbolic object representing the Fourier trigonometric series.\n",
      "    \n",
      "    Examples\n",
      "    ========\n",
      "    \n",
      "    Computing the Fourier series of $f(x) = x^2$:\n",
      "    \n",
      "    >>> from sympy import fourier_series, pi\n",
      "    >>> from sympy.abc import x\n",
      "    >>> f = x**2\n",
      "    >>> s = fourier_series(f, (x, -pi, pi))\n",
      "    >>> s1 = s.truncate(n=3)\n",
      "    >>> s1\n",
      "    -4*cos(x) + cos(2*x) + pi**2/3\n",
      "    \n",
      "    Shifting of the Fourier series:\n",
      "    \n",
      "    >>> s.shift(1).truncate()\n",
      "    -4*cos(x) + cos(2*x) + 1 + pi**2/3\n",
      "    >>> s.shiftx(1).truncate()\n",
      "    -4*cos(x + 1) + cos(2*x + 2) + pi**2/3\n",
      "    \n",
      "    Scaling of the Fourier series:\n",
      "    \n",
      "    >>> s.scale(2).truncate()\n",
      "    -8*cos(x) + 2*cos(2*x) + 2*pi**2/3\n",
      "    >>> s.scalex(2).truncate()\n",
      "    -4*cos(2*x) + cos(4*x) + pi**2/3\n",
      "    \n",
      "    Computing the Fourier series of $f(x) = x$:\n",
      "    \n",
      "    This illustrates how truncating to the higher order gives better\n",
      "    convergence.\n",
      "    \n",
      "    .. plot::\n",
      "        :context: reset\n",
      "        :format: doctest\n",
      "        :include-source: True\n",
      "    \n",
      "        >>> from sympy import fourier_series, pi, plot\n",
      "        >>> from sympy.abc import x\n",
      "        >>> f = x\n",
      "        >>> s = fourier_series(f, (x, -pi, pi))\n",
      "        >>> s1 = s.truncate(n = 3)\n",
      "        >>> s2 = s.truncate(n = 5)\n",
      "        >>> s3 = s.truncate(n = 7)\n",
      "        >>> p = plot(f, s1, s2, s3, (x, -pi, pi), show=False, legend=True)\n",
      "    \n",
      "        >>> p[0].line_color = (0, 0, 0)\n",
      "        >>> p[0].label = 'x'\n",
      "        >>> p[1].line_color = (0.7, 0.7, 0.7)\n",
      "        >>> p[1].label = 'n=3'\n",
      "        >>> p[2].line_color = (0.5, 0.5, 0.5)\n",
      "        >>> p[2].label = 'n=5'\n",
      "        >>> p[3].line_color = (0.3, 0.3, 0.3)\n",
      "        >>> p[3].label = 'n=7'\n",
      "    \n",
      "        >>> p.show()\n",
      "    \n",
      "    This illustrates how the series converges to different sawtooth\n",
      "    waves if the different ranges are specified.\n",
      "    \n",
      "    .. plot::\n",
      "        :context: close-figs\n",
      "        :format: doctest\n",
      "        :include-source: True\n",
      "    \n",
      "        >>> s1 = fourier_series(x, (x, -1, 1)).truncate(10)\n",
      "        >>> s2 = fourier_series(x, (x, -pi, pi)).truncate(10)\n",
      "        >>> s3 = fourier_series(x, (x, 0, 1)).truncate(10)\n",
      "        >>> p = plot(x, s1, s2, s3, (x, -5, 5), show=False, legend=True)\n",
      "    \n",
      "        >>> p[0].line_color = (0, 0, 0)\n",
      "        >>> p[0].label = 'x'\n",
      "        >>> p[1].line_color = (0.7, 0.7, 0.7)\n",
      "        >>> p[1].label = '[-1, 1]'\n",
      "        >>> p[2].line_color = (0.5, 0.5, 0.5)\n",
      "        >>> p[2].label = '[-pi, pi]'\n",
      "        >>> p[3].line_color = (0.3, 0.3, 0.3)\n",
      "        >>> p[3].label = '[0, 1]'\n",
      "    \n",
      "        >>> p.show()\n",
      "    \n",
      "    Notes\n",
      "    =====\n",
      "    \n",
      "    Computing Fourier series can be slow\n",
      "    due to the integration required in computing\n",
      "    an, bn.\n",
      "    \n",
      "    It is faster to compute Fourier series of a function\n",
      "    by using shifting and scaling on an already\n",
      "    computed Fourier series rather than computing\n",
      "    again.\n",
      "    \n",
      "    e.g. If the Fourier series of ``x**2`` is known\n",
      "    the Fourier series of ``x**2 - 1`` can be found by shifting by ``-1``.\n",
      "    \n",
      "    See Also\n",
      "    ========\n",
      "    \n",
      "    sympy.series.fourier.FourierSeries\n",
      "    \n",
      "    References\n",
      "    ==========\n",
      "    \n",
      "    .. [1] https://mathworld.wolfram.com/FourierSeries.html\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(fourier_series)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "16d7f92b",
   "metadata": {},
   "outputs": [],
   "source": [
    "s=fourier_series(x**2,(x,-pi,pi))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "f3ead438",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on method truncate in module sympy.series.fourier:\n",
      "\n",
      "truncate(n=3) method of sympy.series.fourier.FourierSeries instance\n",
      "    Return the first n nonzero terms of the series.\n",
      "    \n",
      "    If ``n`` is None return an iterator.\n",
      "    \n",
      "    Parameters\n",
      "    ==========\n",
      "    \n",
      "    n : int or None\n",
      "        Amount of non-zero terms in approximation or None.\n",
      "    \n",
      "    Returns\n",
      "    =======\n",
      "    \n",
      "    Expr or iterator :\n",
      "        Approximation of function expanded into Fourier series.\n",
      "    \n",
      "    Examples\n",
      "    ========\n",
      "    \n",
      "    >>> from sympy import fourier_series, pi\n",
      "    >>> from sympy.abc import x\n",
      "    >>> s = fourier_series(x, (x, -pi, pi))\n",
      "    >>> s.truncate(4)\n",
      "    2*sin(x) - sin(2*x) + 2*sin(3*x)/3 - sin(4*x)/2\n",
      "    \n",
      "    See Also\n",
      "    ========\n",
      "    \n",
      "    sympy.series.fourier.FourierSeries.sigma_approximation\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(s.truncate)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "f8dbc884",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle - 4 \\cos{\\left(x \\right)} + \\cos{\\left(2 x \\right)} - \\frac{4 \\cos{\\left(3 x \\right)}}{9} + \\frac{\\cos{\\left(4 x \\right)}}{4} - \\frac{4 \\cos{\\left(5 x \\right)}}{25} + \\frac{\\cos{\\left(6 x \\right)}}{9} - \\frac{4 \\cos{\\left(7 x \\right)}}{49} + \\frac{\\pi^{2}}{3}$"
      ],
      "text/plain": [
       "-4*cos(x) + cos(2*x) - 4*cos(3*x)/9 + cos(4*x)/4 - 4*cos(5*x)/25 + cos(6*x)/9 - 4*cos(7*x)/49 + pi**2/3"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s.truncate(8)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "55d4a233",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
